package com.zmn.plat.business.impl.price;

import com.zmn.common.utils.string.StringUtil;
import com.zmn.plat.common.constant.ProductConsts;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 描述: 价格工具类
 *
 * @author guoqiao
 * @since 2021/9/3 17:13
 */
public class PriceUtil {

    private static final int HUNDRED = 100;

    /**
     * 根据基础价*系数计算服务项城市价，计算价格遵循689原则：0≤x≤2，取9（十位数－1），3≤x≤6，取6，7≤x≤8，取8，x=9时，取9
     *
     * @param basicPrice 基础价
     * @param ratio      系数
     * @param bizType    业务类型，F端（bizType=2）价格不采用689
     * @return integer integer
     */
    public static Integer getPriceBy689(Integer basicPrice, Float ratio, Integer bizType) {

        if (basicPrice == null) {
            return 0;
        }
        if (!isPositive(ratio)) {
            return basicPrice;
        }

        BigDecimal temPrice = new BigDecimal(mul(basicPrice.toString(), Float.toString(ratio))).setScale(0, BigDecimal.ROUND_HALF_UP);
        Integer price = temPrice.divide(new BigDecimal(HUNDRED)).setScale(0, BigDecimal.ROUND_HALF_UP).intValue();
        if (price != null && price > 0 && !Objects.equals(bizType, ProductConsts.BIZ_TYPE_F)) {
            if (price <= 2) {
                return price * HUNDRED;
            }
            String priceStr = price.toString();
            Integer last = Integer.valueOf(priceStr.substring(priceStr.length() - 1));
            switch (last) {
                case 0:
                    price = price - 1;
                    break;
                case 1:
                    price = price - 2;
                    break;
                case 2:
                    price = price - 3;
                    break;
                case 3:
                case 4:
                case 5:
                case 6:
                    price = splicePrice(priceStr, 6);
                    break;
                case 7:
                case 8:
                    price = splicePrice(priceStr, 8);
                    break;
                case 9:
                    price = splicePrice(priceStr, 9);
                    break;
                default:
                    break;
            }
        }
        return price * HUNDRED;
    }

    /**
     * 描述: 把数字最后位替换
     *
     * @param priceStr 金额
     * @param last 最后位数
     * @author guoqiao
     * @since 2021/5/12 15:41
     */
    private static Integer splicePrice(String priceStr, Integer last) {
        priceStr = priceStr.substring(0, priceStr.length() - 1) + last;
        return Integer.parseInt(priceStr);
    }

    public static boolean isPositive(Float number) {
        if (Objects.isNull(number)) {
            return false;
        }
        return BigDecimal.ZERO.compareTo(new BigDecimal(number)) < 0;
    }

    public static boolean isPositive(Double number) {
        if (Objects.isNull(number)) {
            return false;
        }
        return BigDecimal.ZERO.compareTo(new BigDecimal(number)) < 0;
    }

    /**
     * 提供精确的乘法运算
     */
    private static double mul(String dou1, String dou2) {
        BigDecimal big1 = new BigDecimal(dou1);
        BigDecimal big2 = new BigDecimal(dou2);
        return big1.multiply(big2).doubleValue();
    }

    public static void main(String[] args) {
        Integer price1 = getPriceBy689(1000, (float) 1.0, 1);
        Integer price2 = getPriceBy689(1100, (float) 1.0, 1);
        Integer price3 = getPriceBy689(1200, (float) 1.0, 1);
        Integer price4 = getPriceBy689(1300, (float) 1.0, 1);
        Integer price5 = getPriceBy689(1420, (float) 1.0, 1);
        Integer price6 = getPriceBy689(1500, (float) 1.0, 1);
        Integer price7 = getPriceBy689(1600, (float) 1.0, 1);
        Integer price8 = getPriceBy689(1700, (float) 1.0, 1);
        Integer price9 = getPriceBy689(1800, (float) 1.0, 1);
        Integer price10 = getPriceBy689(1900, (float) 1.0, 1);
        Integer price = getPriceBy689(2000, (float) 1.0, 1);
        System.out.println(price);

        Integer partPrice = 10000;
        partPrice = PriceUtil.getPriceBy689(partPrice, (float) 1.1, 1);
        partPrice = PriceUtil.getPriceBy689(partPrice, (float) 1, 1);
        partPrice = PriceUtil.getPriceBy689(partPrice, (float) 0.9, 1);
        System.out.println(partPrice);

        List<Integer> stringList = StringUtil.split("11,22,33,44", Integer.class);
        List<Integer> faultIdList = Arrays.asList(11,22,3);
        if (stringList.containsAll(faultIdList)) {
            System.out.println("1111111111");
        } else {
            System.out.println("2222222222");
        }

    }

}
